package com.yenlien.traceyun.business.dao;

import com.yenlien.traceyun.business.entity.StorageManage;
import com.yenlien.traceyun.webase.dao.BaseJpaDao;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.transaction.annotation.Transactional;

import java.util.Collection;
import java.util.List;

/**
 * Created by LiuSha on 2020/7/29.
 */
public interface StorageManageDao extends BaseJpaDao<StorageManage> {

    public StorageManage findByProductNameAndBatch(String productName, String batch);

    StorageManage findById(String id);

    StorageManage findByBatch(String batch);

    @Query(value = "SELECT * FROM t_storage_manage WHERE batch IN (?1) AND company_id = ?2", nativeQuery = true)
    List<StorageManage> findByBatchList(List<String> batchList, String companyId);

    public List<StorageManage> findByIdIn(Collection<String> ids);

    @Query(value = "select product_name from t_plant_storage_manage where company_id = ?1 and source = '采购入库' group by product_name", nativeQuery = true)
    List<String> findSelect(String companyId);

    public List<StorageManage> findBySourceBatchIn(Collection<String> sourceBatchs);

    public List<StorageManage> findByRelateIdIn(Collection<String> relateIds);

    @Transactional
    @Modifying
    @Query(value = "update StorageManage s set s.recall='1' where s.sourceBatch in (?1)")
    public void updateRecallBySourceBatch(Collection<String> sourceBatchs);

    @Transactional
    @Modifying
    @Query(value = "update StorageManage s set s.outNum=(s.outNum+?2) where s.id in (?1)")
    public void updateOutNumByIds(Collection<String> ids, int number);


}
